#ifndef _RESOURCE_REF_HPP_
#define _RESOURCE_REF_HPP_
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

//////////////////////////////////////////////////////////////////////////
// Reference to a resource
//////////////////////////////////////////////////////////////////////////

template< class T >
class ResourceRef
{
protected:
	template< typename AllocatorT >
    ResourceRef(AllocatorT A)
	{
        m_resource = sm_pGlobalResource.lock();
        if (!m_resource.get())
        {
            Reset(A);
        }
	}
	~ResourceRef()
	{
	}

private:
    template< typename AllocatorT >
    void Reset(AllocatorT A)
    {
        m_resource.reset(A());
        sm_pGlobalResource = m_resource;
    }

public:
    T* operator->() const
    {
        ASSERT(m_resource.get()); 
        return m_resource.get();
    }
    T& operator*() const
    {
        ASSERT(m_resource.get()); 
        return m_resource.get();
    }
    T* get() const
    {
        ASSERT(m_resource.get()); 
        return m_resource.get();
    }

public:
    //static int m_iRefCount;
	static boost::weak_ptr<T> sm_pGlobalResource;

protected:
    boost::shared_ptr<T> m_resource;
};

//////////////////////////////////////////////////////////////////////////
template< class T >
boost::weak_ptr<T> ResourceRef<T>::sm_pGlobalResource;

#endif // _RESOURCE_REF_HPP_
